package com.cloud.DrugManagement.service.Impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.DrugManagement.mapper.DrugPriceMapper;
import com.cloud.DrugManagement.mapper.DrugsMapper;
import com.cloud.DrugManagement.mapper.SupplierMapper;
import com.cloud.DrugManagement.pojo.DrugPrice;
import com.cloud.DrugManagement.pojo.Drugs;
import com.cloud.DrugManagement.pojo.Supplier;
import com.cloud.DrugManagement.service.DrugPriceService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhozixiong
 * @date 2023/02/09
 */
@Service
@RequiredArgsConstructor
public class DrugPriceServiceImpl extends ServiceImpl<DrugPriceMapper, DrugPrice> implements DrugPriceService {

    private final static int SIZE = 10;

    private final DrugsMapper drugsMapper;
    private final SupplierMapper supplierMapper;

    @Override
    public Page<DrugPrice> searchAllBySidOrDidOrDname(int pages, int sid, Object parameter) {
        Page page = new Page(pages, SIZE);
        return baseMapper.searchAllBySidOrDidOrDname(page, sid, parameter);
    }

    @Override
    public double searchPriceByDidAndSid(int did, int sid) {
        return baseMapper.searchPriceByDidAndSid(did, sid);
    }

    @Override
    public boolean updatePriceByDidAndSid(int did, int sid, double price) {
        return baseMapper.updatePriceByDidAndSid(did, sid, price) >= 1;
    }

    @Override
    public boolean insert(Integer did, Integer sid, Double retailPrice) {
        Drugs drugs = drugsMapper.searchAllByDid(did);
        Supplier supplier = supplierMapper.searchAllBySid(sid);
        //判断指定商品和供货商是否存在
        if(drugs != null && supplier != null) {
            return baseMapper.insert(did, sid, retailPrice) >= 1;
        }else {
            return false;
        }
    }

    @Override
    public boolean delByDidAndSid(int did, int sid) {
        return baseMapper.delByDidAndSid(did, sid) >= 1;
    }
}
